package finassist.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import finassist.controller.EntityProcessManager;
import finassist.controller.LedgerProcessManager;
import finassist.controller.LoginUserManager;
import finassist.model.Entiti;
import finassist.model.Ledger;
import finassist.model.User;
import finassist.util.Utilities;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * AJ Projects
 * UserLoginAction.java
 * Purpose: Action class for User Login Module. This class takes care of all login related actions.
 *
 * @author Karthick Natarajan
 * @version 1.0 9/10/13
 */

public class LedgerManageAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 9149826260758390091L;
	private Ledger ledger;
	private Long id;
	private Map session;
	private LedgerProcessManager linkController;
	private String role;
	

	public LedgerManageAction() {
		linkController = new LedgerProcessManager();
	}

	public String execute() {
			return SUCCESS;
	}

	public String createLedger() {
		
		Date date;
		Utilities utilities =  new Utilities();
		ledger = getLedger();
		System.out.println(getLedger());
	
		try {
					
			User user1 = (User) ActionContext.getContext().getSession().get("sesUser");
			Entiti entiti = (Entiti) ActionContext.getContext().getSession().get("entiti");
			role = user1.getRole();
			ledger.setCreatedBy(user1.getUsername());
			ledger.setCreateDate(utilities.currentDateTime());
			ledger.setEntitiId(entiti.getId());
			linkController.createLedger(ledger); 
        } catch (Exception e) {
			e.printStackTrace();
		}
		
		return "created";
	}

public String openEntity() {
		
		try {
					
			User user1 = (User) ActionContext.getContext().getSession().get("sesUser");
			role = user1.getRole();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "opened";
	}

	
	
	public static List<Long> getKeysFromValue(Map<Long, String> hm, String value) {
		List<Long> list = new ArrayList<Long>();
		for (Long o : hm.keySet()) {
			if (hm.get(o).equals(value)) {
				list.add(o);
			}
		}
		return list;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;

	}
	
	public Ledger getLedger() {
		return ledger;
	}

	public void setLedger(Ledger ledger) {
		this.ledger = ledger;
	}
	
	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
	
}
